package com.chuanyang.driverh.mainhome.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.mainhome.OrderDetailRceiveActivity;
import com.chuanyang.driverh.mainhome.WaybilListActivity;
import com.chuanyang.driverh.mainorder.SureUnLoadOverActivity;
import com.chuanyang.driverh.mainother.BeforeNaviActivity;
import com.chuanyang.driverh.response2.ResultWaitTransDriver;
import com.chuanyang.driverh.responseresult.OrderAddressResult;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.responseresult.ValidateCodeResult;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.eventbus.EventBusUtil;
import com.chuanyang.driverh.utils.eventbus.RefleshFragEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

import static com.chuanyang.driverh.R.id.tv_bid_price;


public class WaitTransAdapter extends BaseListAdapter<ResultWaitTransDriver.OutDataEntity.DataEntity> {//首页竞价大厅的adapter
    //    private ViewHolder viewHolder;
    private Activity activity;
    private Dialog dialog;
    private AlertDialog alertDialog;
    private EditText et_sureunload_code;
    private TextView tv_cancle_unload;
    private TextView tv_sureunload_getcode;
    private TextView tv_sure_unload;
    private boolean validateOk;
    private String phoneNum;
/*    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0://确认收货运单，1，订单
                    validateCode((String) msg.obj, 0);
                    break;
                case 1://确认收货运单，1，订单
                    validateCode((String) msg.obj, 1);
                    updteOrderUnload((String) msg.obj);
                    break;

            }
        }
    };*/
    private ArrayList<File> files;
    public WaitTransAdapter(Activity activity) {
        super(activity);//此处不传入集合，则默认集合长度为0，在setdataresult（）方法中，添加数据并刷新
        this.activity = activity;
    }

    @Override
    public View getZkView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = getInflater().inflate(R.layout.home_item_wait_trans, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.line_left = convertView.findViewById(R.id.line_left);
            viewHolder.tv_order_status = (TextView) convertView.findViewById(R.id.tv_order_status);
            viewHolder.tv_order_no = (TextView) convertView.findViewById(R.id.tv_order_no);
            viewHolder.tv_order_time = (TextView) convertView.findViewById(R.id.tv_order_time);
            viewHolder.tv_city_start = (TextView) convertView.findViewById(R.id.tv_city_start);
            viewHolder.tv_city_end = (TextView) convertView.findViewById(R.id.tv_city_end);
            viewHolder.tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
            viewHolder.tv_acture_weight_num = (TextView) convertView.findViewById(R.id.tv_acture_weight_num);
            viewHolder.tv_guapai_weight_num = (TextView) convertView.findViewById(R.id.tv_guapai_weight_num);
            viewHolder.tv_load = (TextView) convertView.findViewById(R.id.tv_load);
            viewHolder.tv_time_load = (TextView) convertView.findViewById(R.id.tv_time_load);
            viewHolder.tv_bid_price = (TextView) convertView.findViewById(tv_bid_price);
            viewHolder.tv_unload = (TextView) convertView.findViewById(R.id.tv_unload);
            viewHolder.tv_time_unload = (TextView) convertView.findViewById(R.id.tv_time_unload);
            viewHolder.tv_total_price = (TextView) convertView.findViewById(R.id.tv_total_price);
            viewHolder.tv_see_waibil = (TextView) convertView.findViewById(R.id.tv_see_waibil);
            viewHolder.tv_complete_order = (TextView) convertView.findViewById(R.id.tv_complete_order);
            viewHolder.tv_order_type = (TextView) convertView.findViewById(R.id.tv_order_type);
            viewHolder.ll_div = (LinearLayout) convertView.findViewById(R.id.ll_div);
            viewHolder.tv_msg = (TextView) convertView.findViewById(R.id.tv_msg);
            viewHolder.tv_notice_no = (TextView) convertView.findViewById(R.id.tv_notice_no);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();

        }
        final ResultWaitTransDriver.OutDataEntity.DataEntity data = getItem(position);

        //发货通知单号
        if (StringUtil.isBlank(data.getNoticeNo())){
            viewHolder.tv_notice_no.setVisibility(View.GONE);
        }else {
            viewHolder.tv_notice_no.setVisibility(View.VISIBLE);
            if (data.getNoticeNo().contains(",")){
                viewHolder.tv_notice_no.setText(data.getNoticeNo().substring(0,data.getNoticeNo().indexOf(","))+"...");
            }else {
                viewHolder.tv_notice_no.setText(data.getNoticeNo());
            }
        }

        if (data.getOrderStatus() <= 3) {
            viewHolder.tv_order_status.setText("待装货");
            viewHolder.tv_order_status.setTextColor(activity.getResources().getColor(R.color.gray_66));
            viewHolder.line_left.setBackgroundColor(activity.getResources().getColor(R.color.gray_66));
            if (data.getFleetId() != 0) {
                viewHolder.tv_see_waibil.setText("查看运单");
                viewHolder.tv_see_waibil.setVisibility(View.VISIBLE);
                viewHolder.tv_complete_order.setVisibility(View.GONE);
            } else {
              /*  viewHolder.tv_see_waibil.setText("导航");
                viewHolder.tv_complete_order.setText("开始运输");
                viewHolder.tv_see_waibil.setVisibility(View.GONE);
                viewHolder.tv_complete_order.setVisibility(View.VISIBLE);*/


                viewHolder.tv_see_waibil.setText("查看运单");
                viewHolder.tv_see_waibil.setVisibility(View.VISIBLE);
                viewHolder.tv_complete_order.setVisibility(View.GONE);
            }
        } else if (data.getOrderStatus() == 4) {
            viewHolder.tv_order_status.setText("运输中");
            viewHolder.tv_order_status.setTextColor(activity.getResources().getColor(R.color.text_assign_car_green));
            viewHolder.line_left.setBackgroundColor(activity.getResources().getColor(R.color.text_assign_car_green));
            if (data.getFleetId() != 0) {
                viewHolder.tv_see_waibil.setText("查看运单");
                viewHolder.tv_complete_order.setText("完成订单");
                viewHolder.tv_see_waibil.setVisibility(View.VISIBLE);
                viewHolder.tv_complete_order.setVisibility(View.VISIBLE);
            } else {
               /* viewHolder.tv_see_waibil.setText("导航");
//                viewHolder.tv_complete_order.setText("确认卸货");
                viewHolder.tv_complete_order.setText("运输完成");
                viewHolder.tv_see_waibil.setVisibility(View.VISIBLE);
                viewHolder.tv_complete_order.setVisibility(View.VISIBLE);*/

                viewHolder.tv_see_waibil.setText("查看运单");
                viewHolder.tv_see_waibil.setVisibility(View.VISIBLE);
                viewHolder.tv_complete_order.setVisibility(View.GONE);
            }

        } else if (data.getOrderStatus() == 5) {
            viewHolder.tv_order_status.setTextColor(activity.getResources().getColor(R.color.text_assign_car_green));
            viewHolder.line_left.setBackgroundColor(activity.getResources().getColor(R.color.text_assign_car_green));
            viewHolder.tv_order_status.setText("运输中");
            if (data.getFleetId() != 0) {
                viewHolder.tv_see_waibil.setText("查看运单");
                viewHolder.tv_complete_order.setText("完成订单");
                viewHolder.tv_see_waibil.setVisibility(View.VISIBLE);
                viewHolder.tv_complete_order.setVisibility(View.VISIBLE);
            } else {
               /* viewHolder.tv_see_waibil.setText("导航");
                viewHolder.tv_complete_order.setText("运输完成");
                viewHolder.tv_see_waibil.setVisibility(View.VISIBLE);
                viewHolder.tv_complete_order.setVisibility(View.VISIBLE);*/


                viewHolder.tv_see_waibil.setText("查看运单");
                viewHolder.tv_see_waibil.setVisibility(View.VISIBLE);
                viewHolder.tv_complete_order.setVisibility(View.GONE);
            }
        } else if (data.getOrderStatus() == 6) {
            viewHolder.tv_order_status.setText("待收货");
            viewHolder.tv_order_status.setTextColor(activity.getResources().getColor(R.color.text_get_order_orange));
            viewHolder.line_left.setBackgroundColor(activity.getResources().getColor(R.color.text_get_order_orange));
            if (data.getFleetId() != 0) {
                viewHolder.tv_see_waibil.setText("查看运单");
                viewHolder.tv_see_waibil.setVisibility(View.VISIBLE);
                viewHolder.tv_complete_order.setVisibility(View.GONE);
            } else {
              /*  viewHolder.tv_see_waibil.setText("导航");
                viewHolder.tv_complete_order.setText("验证卸货");
                viewHolder.tv_see_waibil.setVisibility(View.GONE);
                viewHolder.tv_complete_order.setVisibility(View.VISIBLE);*/
                viewHolder.tv_see_waibil.setText("查看运单");
                viewHolder.tv_see_waibil.setVisibility(View.VISIBLE);
                viewHolder.tv_complete_order.setVisibility(View.GONE);

            }
        } else if (data.getOrderStatus() > 6) {
            viewHolder.tv_order_status.setText("已完成");
            viewHolder.tv_order_status.setTextColor(activity.getResources().getColor(R.color.text_assign_car_blue));
            viewHolder.line_left.setBackgroundColor(activity.getResources().getColor(R.color.text_assign_car_blue));
            if (data.getFleetId() != 0) {
                viewHolder.tv_see_waibil.setText("查看运单");
                viewHolder.tv_see_waibil.setVisibility(View.VISIBLE);
                viewHolder.tv_complete_order.setVisibility(View.GONE);
            } else {
               /* viewHolder.tv_see_waibil.setVisibility(View.GONE);
                viewHolder.tv_complete_order.setVisibility(View.GONE);*/

                viewHolder.tv_see_waibil.setText("查看运单");
                viewHolder.tv_see_waibil.setVisibility(View.VISIBLE);
                viewHolder.tv_complete_order.setVisibility(View.GONE);
            }


        }
        viewHolder.tv_order_no.setText(data.getOrderNo());
//        viewHolder.tv_order_time.setText(data.getOrderTime());

        if (data.getCreateTime() != null) {
            viewHolder.tv_order_time.setText(data.getCreateTime());
        } else {
            viewHolder.tv_order_time.setText(data.getOrderTime());
        }











        viewHolder.tv_city_start.setText(data.getStartPoint());
        viewHolder.tv_city_end.setText(data.getEndPoint());

//        viewHolder.tv_goods_name.setText(data.getGoodsName());
        if (data.getOrderDetail() != null && data.getOrderDetail().getGoodsName() != null) {
            viewHolder.tv_goods_name.setText(data.getOrderDetail().getGoodsName());
        } else {
            viewHolder.tv_goods_name.setText(data.getGoodsName());
        }


//        viewHolder.tv_acture_weight_num = (TextView) convertView.findViewById(R.id.tv_acture_weight_num);
//        viewHolder.tv_guapai_weight_num = (TextView) convertView.findViewById(R.id.tv_guapai_weight_num);
        String priceUnit = data.getCurrency() == 1 ? "¥" : "$";
        if (data.getOrderStatus() <= 3) {//还没运输
            viewHolder.tv_acture_weight_num.setVisibility(View.GONE);
            viewHolder.tv_guapai_weight_num.setText("挂牌: " + StringUtil.getDouble(data.getSumSrplusWeight() + "") + "t/" + data.getSumSurplusNum() + "件");
            viewHolder.tv_guapai_weight_num.getPaint().setFlags(0);
            viewHolder.tv_total_price.setText("总价: 待更新");

        } else {//运输中
            viewHolder.tv_acture_weight_num.setVisibility(View.VISIBLE);
            viewHolder.tv_acture_weight_num.setText("实发: " + StringUtil.getDouble(data.getSumDetailsWeight() + "") + "t/" + data.getSumDetailsNum() + "件");
            viewHolder.tv_guapai_weight_num.setText("挂牌: " + StringUtil.getDouble(data.getSumSrplusWeight() + "") + "t/" + data.getSumSurplusNum() + "件");
            viewHolder.tv_guapai_weight_num.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            viewHolder.tv_total_price.setText("总价" + StringUtil.getDouble(data.getSumPrice() + "") + priceUnit);
        }
        viewHolder.tv_bid_price.setText("挂牌: " + StringUtil.getDouble(data.getNormalPrice() + "") + priceUnit);
        viewHolder.tv_bid_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);


       /* viewHolder.tv_time_load.setText(data.getOrderStartime());
        if (StringUtil.isBlank(data.getOrderEndtime())) {
            viewHolder.tv_time_unload.setText("不限");
        } else {
            viewHolder.tv_time_unload.setText(data.getOrderEndtime());
        }*/

        if (data.getTransPortWay() == 2)//船
        {
            viewHolder.tv_load.setText("装卸天数");
            viewHolder.tv_unload.setText("受载日期");
            viewHolder.tv_load.setPadding(6,0,6,0);
            viewHolder.tv_unload.setPadding(6,0,6,0);
            viewHolder.tv_load.setBackgroundResource(R.drawable.tv_shape_round_corner_load);
            viewHolder.tv_unload.setBackgroundResource(R.drawable.tv_shape_round_corner_unload);
            viewHolder.tv_time_load.setText(data.getStevedoreDay()+"天");
            viewHolder.tv_time_unload.setText(data.getLoadDate()+"±"+data.getFq()+"天");
        }else {
            viewHolder.tv_load.setText("提");
            viewHolder.tv_unload.setText("卸");
            viewHolder.tv_load.setPadding(0,0,0,0);
            viewHolder.tv_unload.setPadding(0,0,0,0);
            viewHolder.tv_load.setBackgroundResource(R.drawable.tv_point_load);
            viewHolder.tv_unload.setBackgroundResource(R.drawable.tv_point_unload);
            viewHolder.tv_time_load.setText(data.getOrderStartime());
            if (!StringUtil.isBlank(data.getOrderEndtime())) {
                viewHolder.tv_time_unload.setText(data.getOrderEndtime());
            }else {
                viewHolder.tv_time_unload.setText("不限");
            }

        }



      /* if (data.getCurrency()==1){
           viewHolder.tv_bid_price.setText("挂牌 " + StringUtil.getDouble(data.getNormalPrice() + "") + "¥");
           viewHolder.tv_total_price.setText("总价" + StringUtil.getDouble(data.getSumPrice() + "") + "¥");
       }else {
           viewHolder.tv_bid_price.setText("挂牌 " + StringUtil.getDouble(data.getNormalPrice() + "") + "$");
           viewHolder.tv_total_price.setText("总价" + StringUtil.getDouble(data.getSumPrice() + "") + "$");
       }*/

        if (data.getOrderType() == 1) {//1竞价
            if (data.getFleetId() != 0) {
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_bid_t);
            } else {
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_bid_p);
            }

        } else if (data.getOrderType() == 2) {//匹配

            if (data.getFleetId() != 0) {
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_match_t);
            } else {
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_match_p);
            }
        } else {//专线

            if (data.getFleetId() != 0) {
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_special_t);
            } else {
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_special_p);
            }
        }

        final ViewHolder finalViewHolder = viewHolder;

        finalViewHolder.tv_see_waibil.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (data.getFleetId() != 0) {
                    Intent intent = new Intent(activity, WaybilListActivity.class);
                    intent.putExtra("data", data);
//                    intent.putExtra("data", data.getOrderId()+"");
//                    intent.putExtra("begin", data.getStartPoint());
//                    intent.putExtra("end", data.getEndPoint());

                    activity.startActivity(intent);
                } else {
                    //导航
//                    selectOrderAddress(data.getOrderId() + "");


                    Intent intent = new Intent(activity, WaybilListActivity.class);
                    intent.putExtra("data", data);
                    activity.startActivity(intent);


                }
            }
        });
        finalViewHolder.tv_complete_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (data.getFleetId() != 0) {
                    if (data.getOrderStatus() < 4) {
                        PrintUtil.toast(activity, "当前订单无法执行此操作");
                        return;
                    }
                    dialog = DialogUtil.myDialog(activity, "完成订单", "取消", "确定", "确定完成订单？", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (v.getId() == R.id.dialog_btn_right) {
                                completeOrder(data);
                            }
                            dialog.cancel();
                        }
                    });
                } else {

                    if (data.getOrderStatus() <= 3) {
                        //开始运输
//                        startTranslate(data.getPageWayBillList().get(0));
                        startTranslate(data.getWaybillId() + "");
                    } else if (data.getOrderStatus() == 4) {
                        //确认卸货

                        Intent intent = new Intent(activity, SureUnLoadOverActivity.class);
                        if (data.getFleetId() == 0) {
                            intent.putExtra("orederId", data.getOrderId());
                            intent.putExtra("fromlist", true);
                            intent.putExtra("personalOrder", true);
                        } else {
                            intent.putExtra("waybillId", data.getWaybillId());
                            intent.putExtra("fromlist", true);
                            intent.putExtra("personalOrder", false);
                        }
                        activity.startActivity(intent);


                    } else if (data.getOrderStatus() <= 5) {
                        //运输完成，即上传卸货照片。
                        Intent intent = new Intent(activity, SureUnLoadOverActivity.class);
                        if (data.getFleetId() == 0) {
                            intent.putExtra("orederId", data.getOrderId());
                            intent.putExtra("fromlist", true);
                            intent.putExtra("personalOrder", true);
                        } else {
                            intent.putExtra("waybillId", data.getWaybillId());
                            intent.putExtra("fromlist", true);
                            intent.putExtra("personalOrder", false);
                        }
                        activity.startActivity(intent);

                    } else if (data.getOrderStatus() == 6) {
                        //验证卸货
                        //所有地方都传运单id
//                        sureAcceptGood(data.getPhoneCall(), data.getWaybillId() + "");

                       /* if (data.getFleetId() != 0) {
                            sureAcceptGood(data.getPhoneCall(), data.getWaybillId() + "", 0);
                        } else {
                            sureAcceptGood(data.getPhoneCall(), data.getOrderId() + "", 1);
                        }*/
                        //此处只有车队长接的个人单。
                        sureAcceptGood(data.getPhoneCall(), data.getOrderId() + "", 1);


                    } else {

                    }
                }
            }
        });

        viewHolder.ll_div.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, OrderDetailRceiveActivity.class);
                intent.putExtra("data", data.getOrderId() + "");
                intent.putExtra("type", "0");

                if (data.getFleetId() != 0) {
                    if (data.getOrderStatus() < 6 && data.getOrderStatus() > 3) {
                        intent.putExtra("type", "8");
                    } else {
                        intent.putExtra("type", "9");
                    }
                } else {
                    intent.putExtra("type", "10");

                }

                intent.putExtra("waitTrasData", data);
                activity.startActivity(intent);
            }
        });

//5.4日改成不显示。
        viewHolder.tv_bid_price.setVisibility(View.INVISIBLE);
        viewHolder.tv_total_price.setVisibility(View.VISIBLE);
        viewHolder.tv_total_price.setText("单价:" + StringUtil.getDouble(data.getPrice() + "") + priceUnit + "/t");


        return convertView;
    }



    private void completeOrder(ResultWaitTransDriver.OutDataEntity.DataEntity data) {
        DialogUtil.showLoadingDialog(activity, "请求中...");
        Map<String, String> params = new HashMap<String, String>();
        params.put("orderId", data.getOrderId() + "");
        params.put("orderStatus", "6");
        MyHttpUtil.updateOrderStatusByWaybill(activity, params, new MyCallback<Result>(activity, new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                PrintUtil.toast(activity, "网络异常，请稍后再试");
                e.printStackTrace();
            }

            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    EventBusUtil.postEvent(new RefleshFragEvent(3));

                } else {
                    if (result.getMsg() != null) {
                        PrintUtil.toast(activity, result.getMsg());
                    } else {
                        PrintUtil.toast(activity, "目前有订单正在运输，尚不能完成订单");
                    }
                }
            }
        });
    }

    /**
     * 导航请求数据
     *
     * @param id
     */
    private void selectOrderAddress(String id) {
        DialogUtil.showLoadingDialog(activity, "请求中...");
        Map<String, String> params = new HashMap<String, String>();
        params.put("orderId", id);
        MyHttpUtil.selectOrderAddress(activity, params, new MyCallback<OrderAddressResult>(activity, new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                PrintUtil.toast(activity, "网络异常，请稍后再试");
                e.printStackTrace();
            }

            @Override
            public void onSuccess(OrderAddressResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    Intent intent = new Intent(activity, BeforeNaviActivity.class);
                    intent.putExtra("OrderAddressResult", result);
                    activity.startActivity(intent);
                } else {
                    PrintUtil.toast(activity, result.getMsg());
                }
            }
        });
    }

    private void startTranslate(String waybillId) {
        DialogUtil.showLoadingDialog(activity, "请求中...");
        Map<String, String> params = new HashMap<String, String>();
        params.put("waybillId", waybillId + "");
        params.put("waybillStatus", "4");
        params.put("driverId", SharedPrefUtil.getUserId());
        MyHttpUtil.startTranslate(activity, params, new MyCallback<Result>(activity, new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                PrintUtil.toast(activity, "网络异常，请稍后再试");
                e.printStackTrace();
            }

            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    EventBusUtil.postEvent(new RefleshFragEvent(3));
                } else {
                    PrintUtil.toast(activity, result.getMsg());
                }
            }
        });


    }


    /**
     * 验证卸货
     *
     * @param phone id
     */
    public void sureAcceptGood(final String phone, final String id, final int type) {
        if (StringUtil.isBlank(phone)) {
            PrintUtil.toast(activity, "收货人电话为空");
            return;
        }
        if (null == alertDialog) {
            alertDialog = new AlertDialog.Builder(activity).create();
            alertDialog.show();
            alertDialog.setTitle(null);
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

                @Override
                public void onDismiss(DialogInterface dialog) {
                    // TODO Auto-generated method stub
                    alertDialog = null;
                }
            });
            Window window = alertDialog.getWindow();
            window.setGravity(Gravity.CENTER);
            window.setBackgroundDrawable(new ColorDrawable(0));
            window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            window.setContentView(R.layout.dialog_sureunload);
            View view = window.getDecorView();
            et_sureunload_code = (EditText) view.findViewById(R.id.et_sureunload_code);
            tv_cancle_unload = (TextView) view.findViewById(R.id.tv_cancle_unload);
            tv_sureunload_getcode = (TextView) view.findViewById(R.id.tv_sureunload_getcode);
            tv_sure_unload = (TextView) view.findViewById(R.id.tv_sure_unload);
            alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    validateOk = false;

                }
            });
            tv_cancle_unload.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    alertDialog.dismiss();
                    alertDialog = null;
                }
            });
            tv_sureunload_getcode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!validateOk) {//获取成功验证码 且对话框没消失时无法多次点击
                        getValidateCode(phone, id);
                    } else {

                        PrintUtil.toast(activity, "验证码已发送给货主！");
                    }
                }
            });
            tv_sure_unload.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (TextUtils.isEmpty(et_sureunload_code.getText().toString().trim())) {
                        PrintUtil.toast(activity, "请填写获取到验证码");
                        return;
                    }
                    if (StringUtil.isBlank(phoneNum)) {
                        PrintUtil.toast(activity, "请先获取验证码");
                        return;
                    }
                    updteOrderUnload(id);

//                    Message message = Message.obtain();
//                    message.obj = id;
//                    message.what = type;
//                    mHandler.sendMessage(message);
//                    alertDialog.dismiss();
//                    alertDialog = null;
                }
            });
        }
        //下面两行代码加入后即可弹出输入法
        alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }


    /**
     * 获取验证码
     */
    private void getValidateCode(final String phone, String id) {
        DialogUtil.showLoadingDialog(activity, "加载中...");
        Map<String, String> params = new HashMap<String, String>();
        params.put("phone", phone);
        params.put("validateMsg", id);
        MyHttpUtil.getSureValidateCode(activity, params, new MyCallback<ValidateCodeResult>(activity, new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activity, "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(ValidateCodeResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    phoneNum = phone;
                    validateOk = true;
                    tv_sureunload_getcode.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.tv_shape_gray_round_corner_solid));
                } else {
                    PrintUtil.toast(activity, result.getMsg());
                }
            }
        });


    }
    /**
     * 代替货主收货接口 (个人订单时)
     */
    private void updteOrderUnload(String orderId) {
        DialogUtil.showLoadingDialog(activity,"请求中...");
        Map<String,String> params=new HashMap<String, String>();
        if (files == null) {
            files = new ArrayList();
        }
        params.clear();
        params.put("orderId", orderId + "");
        params.put("orderStatus", "7");
        params.put("validateMsg", orderId + "");
        params.put("validateCode", et_sureunload_code.getText().toString().trim());
        MyHttpUtil.updateOrderUnload(activity,files,params, new MyCallback<Result>(activity,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activity, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    PrintUtil.toast(activity, "您已替货主确认收货");
                    EventBusUtil.postEvent(new RefleshFragEvent(3));
                    alertDialog.dismiss();
                    alertDialog = null;
                }else{
                    PrintUtil.toast(activity,result.getMsg());
                }
            }
        });
    }

    /**
     * 收货完成 （传入订单id或者运单id）
     */
   /* private void updateWaybillUnload(String id, int type) {//type0运单，1订单
        DialogUtil.showLoadingDialog(activity, "加载中...");
        Map<String, Object> params = new HashMap<String, Object>();
        if (type == 0) {
            params.put("waybillId", id);
            params.put("waybillStatus", "7");
        } else {
            params.put("orderId", id);
            params.put("orderStatus", "7");
        }
        params.put("validateMsg", id + "");
        params.put("validateCode", et_sureunload_code.getText().toString().trim());
        MyHttpUtil.signoff(activity, params, new MyCallback<Result>(activity, new JsonGenericsSerializator()) {
            //        MyHttpUtil.updateOrderUnload(activity,null,params, new MyCallback<Result>(activity,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activity, "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    PrintUtil.toast(activity, "您已替货主确认收货");
                    EventBusUtil.postEvent(new RefleshFragEvent(3));
                } else {
//                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });


    }*/


   /* private void validateCode(final String orderId, final int type) {
        if (StringUtil.isBlank(phoneNum)) return;
        DialogUtil.showLoadingDialog(activity, "加载中...");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("phone", phoneNum);
        params.put("validateCode", et_sureunload_code.getText().toString().trim());
        MyHttpUtil.validateCode(activity, params, new MyCallback<ValidateCodeIfTrueResult>(activity, new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
            }

            @Override
            public void onSuccess(ValidateCodeIfTrueResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    if (result.getData()) {
                        updateWaybillUnload(orderId, type);
                        alertDialog.dismiss();
                        alertDialog = null;
                    } else {
                        PrintUtil.toast(activity, "验证码错误");
                    }
                } else {
                    if (result.getMsg() != null) {
                        PrintUtil.toast(activity, result.getMsg());
                    } else {
                        PrintUtil.toast(activity, "验证失败");
                    }
                }
            }
        });


    }*/

    class ViewHolder {
        View line_left;
        TextView tv_order_status;
        TextView tv_order_no;
        TextView tv_order_time;
        TextView tv_city_start;
        TextView tv_city_end;
        TextView tv_goods_name;
        TextView tv_acture_weight_num;
        TextView tv_guapai_weight_num;
        TextView tv_load;
        TextView tv_time_load;
        TextView tv_bid_price;
        TextView tv_unload;
        TextView tv_time_unload;
        TextView tv_total_price;
        TextView tv_see_waibil;
        TextView tv_complete_order;
        TextView tv_order_type;
        LinearLayout ll_div;
        TextView tv_msg;
        TextView tv_notice_no;
    }
}
